apiready = function(){
seajs.use(['../../V',"../../api","../../jquery","../../app","../../service","../../swiper","../../dialog"],function(V,$api,$,app,service,Swiper,dialog){
	var vm = {
		el: '.container',
	  	data: {
				showType: false,
				shopInfo: {},
				bannerList: [], // 广告位列表
				discountList: [], // 优惠券列表
				collageList: [], // 拼团列表
				classifyList: [], // 分类列表
				hotGoodsList: [], // 热门商品列表
				goodsList: [], // 全部商品列表
				isCollect: false, // 是否已收藏
				collectId: '', // 收藏id
				introduce: false, // 是否展示全部简介
				shareShow: false, // 是否显示分享
				tipsInfo: {}, // 商城提示消息
				tipsList: [], // 商城提示列表
				tipsMsg: {}, // 提示内容
				otherUserInfo: {},
				userInfo: '', // 获取用户身份
				yzcGoods: [], // yzc商品列表
				bs: "",	//ajax弹出服务器提示控制变量
	  	},
			created: function(){
				var that = this;
				if ($api.getStorage("user")) {
						this.userInfo = $api.getStorage("user").data;
						this.bs = 1;
				}
				var shopData = api.pageParam.shopData;
				var otherUserId = api.pageParam.otherUserInfoId;

				var shopUnionId;
				if (shopData.shop_unionid) {
					shopUnionId = shopData.shop_unionid;
				} else {
					shopUnionId = shopData.unionid;
				}

				var shopOpenId = shopData.openid;
				var shareopenid = shopData.shareopenid;

				// 获取店铺信息
				this.getShopMsg(shopUnionId,otherUserId);

				api.addEventListener({
				    name: 'toCart'
				}, function(ret, err){
				    if( ret ){
				         that.toShoppingCart();
				    }else{
				         alert( JSON.stringify( err ) );
				    }
				});

				// 监听购物车数量改变
				api.addEventListener({
				    name: 'lisentCart'
				}, function(ret, err){
				    if(ret){
							that.getCartCount();
				    }
				});

				api.addEventListener({
				    name: 'shopIndexDoShomeThing'
				}, function(ret, err){
				    if( ret ){
							var type = ret.value.type;
							if (type == 1) {
								// 分享
								that.showShare();
							} else if (type == 0) {
								// 商城首页
								api.openWin({
										name: 'index',
										// customRefreshHeader: "UIPullRefreshMotive",
										url: 'widget://src/Shop/shop_index/shop_index.html',
										pageParam: {
												channel: 'YK'
										}
								})
							} else if (type == 2) {
								// 我的名片
								api.closeToWin({
								    name: 'root',
										animation: {
											type: 'fade',
											subType: 'from_right',
											duration: 300
										}
								});
							}
				    }
				});

				$('.buy-head-cart').on('click', function() {
						if (that.bs == "") { //游客模式，没有缓存
								dialog.alert({
										title: '游客模式',
										msg: '体验更多功能，请先登录',
										buttons: ['取消', '确定']
								}, function(ret) {
										if (ret.buttonIndex == 2) { //确认
												api.closeToWin({
														name: 'WeChat_login'
												});
										}
								})
						} else {
								api.sendEvent({
										name: 'toCart'
								});
						}
				});

	  	},
	  	methods:{
  			init: function () {
					var that = this;
					$api.setTimeout(function(){
						var mySwiper = new Swiper('.swiper-container', {
		          autoplay: true,
		          pagination: {
		            el: '.swiper-pagination'
		          },
		          on: {
		            autoplayStop: function(event){
		              mySwiper.autoplay.start();
		            },
		          }
		        });
					},900);

					// 获取banner列表
					this.getBanner();

					// 获取优惠券列表
					this.getDiscountList();

					// 获取拼团列表
					this.getCollageList();

					// 获取分类列表
					this.getClassifyList();

					// 热门商品列表
					this.getHotGoodsList();

					// 全部商品列表
					this.getAllGoodsList();

					// 获取购物车商品数量
					this.getCartCount();

					// 获取yzc商品列表
					this.getYzcGoodsList();

					// 检车是否已收藏当前店铺
					this.checkIsCollect();

					// 获取商城提示消息
					this.getInformatiom(0);
				},

				// 获取店铺信息
				getShopMsg: function (shopUnionId,otherUserId) {
					var that = this;
					app.ajax(
						service['getOtherShop'],
						{
							data: {
								values: {
									unionid: shopUnionId,
									other_user_id: otherUserId
								}
							}
						},
						function (res) {
							if (res.code == 0) {
								if (res.data.length == 0) {
									api.toast({
									    msg: '该用户还没有店铺',
									    duration: 2000,
									    location: 'bottom'
									});
									$api.setTimeout(function () {
										api.closeWin();
									}, 2000);
								} else {
									that.shopInfo = res.data;
									that.otherUserInfo = res.data.exchange;
									that.init();
								}

							} else {
								api.toast({
								    msg: res.message,
								    duration: 2000,
								    location: 'bottom'
								});
							}
						}
					);
				},

				// 获取购物车数量
				getCartCount: function () {
					var that = this;
					app.ajax(
						service['cartGetCartCount'],
						{
							data: {
								values: {
									unionid: $api.getStorage("shop_unionid"),
									bs: this.bs
								}
							}
						},
						function (res) {
							if (res.code == 0) {
								that.cartCount = res.data;
								if (that.cartCount > 0) {
									that.cartCount > 10 ? $('.js-cart-count').text('10+') : $('.js-cart-count').text(that.cartCount);
									$('.js-cart-count').show();
								} else {
									$('.js-cart-count').hide();
								}

							} else {
								// api.toast({
								//     msg: res.message,
								//     duration: 2000,
								//     location: 'bottom'
								// });
							}
						}
					);
				},

				// 获取商城提示消息
				getInformatiom: function (time_stamp) {
					var that = this;
					app.ajax(
						service['getHomeInformation'],
						{
							data: {
								values: {
									unionid: $api.getStorage("shop_unionid"),
									time_stamp: time_stamp,
									mask: 'none',
									bs: ""
								}
							}
						},
						function (res) {
							if (res.code == 0) {
								that.tipsInfo = res.data;
								that.tipsList = res.data.list;
								that.showTips();
							} else {
								// api.toast({
								//     msg: res.message,
								//     duration: 2000,
								//     location: 'bottom'
								// });
							}
						}
					);
				},

				// 显示商城提示
				showTips: function () {
					var that = this;
					if (this.tipsList.length > 1) {
						this.tipsMsg = {
							avater: that.tipsList[0].avater,
							name: that.tipsList[0].nickname,
							msg: that.tipsList[0].msg,
							goodsId: that.tipsList[0].goods_id,
							groupId: that.tipsList[0].group_id
						};

						this.tipsList = this.tipsList.splice(0,1);
						$('.loop-tips').fadeIn('fast',function () {
							$(this).css('display','flex');
						});
						$api.setTimeout(function () {
							$('.loop-tips').fadeOut();
							that.showTips();
						}, 3000);
					} else {
						$api.setTimeout(function () {
							that.getInformatiom(that.tipsInfo.time_stamp);
						}, 5000);
					}
				},

				toTipsDetail: function (goodsId, groupId) {
					var that = this;
					if (groupId) {
						// 拼团商品
						app.openWin('tipsDetail', {groupId: groupId, goodsId: goodsId});
					} else {
						// 普通商品
						app.openWin('buyGoods', {goodsId: goodsId});
					}
				},

				// 显示分享
				showShare: function () {
					this.shareShow = true;
				},
				hideShare: function () {
					this.shareShow = false;
				},

				// 分享到微信
				shareToWx: function (type) {
					var that = this;
					if (this.bs == "") { //游客模式，没有缓存
							dialog.alert({
									title: '游客模式',
									msg: '体验更多功能，请先登录',
									buttons: ['取消', '确定']
							}, function(ret) {
									if (ret.buttonIndex == 2) { //确认
											api.closeToWin({
													name: 'WeChat_login'
											});
									}
							})
					}else{
						var wx = api.require('wx');
						var toWhere;
						if (type == 1) {
							// 分享给朋友
							toWhere = 'session';
						} else if (type == 2) {
							// 分享到朋友圈
							toWhere = 'timeline';
						}
						app.downloadImg(that.shopInfo.shop_logo, function (img_url) {
							that.getShareLink(function(url) {
								wx.shareWebpage({
								    scene: toWhere,
								    title: '这是我的有张店铺，更多优质产品点击即可',
								    description: '这是我的有张店铺，更多优质产品点击即可',
								    contentUrl: url,
										thumb: img_url
								}, function(ret, err) {
								    if (ret.status) {
								        api.toast({
								            msg: '分享成功',
								            duration: 2000,
								            location: 'bottom'
								        });
								    } else {
											api.toast({
													msg: '分享失败',
													duration: 2000,
													location: 'bottom'
											});
								    }
								});
							});
						});
					}

				},

				// 获取分享连接
				getShareLink: function (callback) {
					var that = this;
					app.ajax(
						service['Share'],
						{
							data: {
								values: {
									type: 1,
									unionid: $api.getStorage("shop_unionid"),
									share_id: that.shopInfo.id
								}
							}
						},
						function (res) {
							if (res.code == 0) {
								var url = res.data;
								callback(url);
							} else {
								api.toast({
								    msg: '获取分享链接失败',
								    duration: 2000,
								    location: 'bottom'
								});
							}
						}
					);
				},

				// 展开简介
				showIntroduce: function () {
					this.introduce = !this.introduce;
				},

				// 获取广告位图片列表
				getBanner: function () {

					var that = this;
					app.ajax(
						service['bannerList'],
						{
							data: {
								values: {
									unionid: that.shopInfo.unionid,
									shop_code: that.shopInfo.shop_code
								}
							}
						},
						function (res) {
							if (res.code == 0) {
								that.bannerList = res.data;
							} else {
								api.toast({
								    msg: res.message,
								    duration: 2000,
								    location: 'bottom'
								});
							}
						}
					);
				},

				// 获取优惠券列表
				getDiscountList: function () {
					var that = this;
					app.ajax(
						service['getCouponActiveList'],
						{
							data: {
								values: {
									unionid: that.shopInfo.unionid,
									shop_code: that.shopInfo.shop_code,
									is_active: 1,
									user_unionid: $api.getStorage("shop_unionid")
								}
							}
						},
						function (res) {
							if (res.code == 0) {
								that.discountList = res.data;
							} else {
								api.toast({
								    msg: res.message,
								    duration: 2000,
								    location: 'bottom'
								});
							}
						}
					);
				},

				// 获取拼团列表
				getCollageList: function () {
					var that = this;
					app.ajax(
						service['getCollageList'],
						{
							data: {
								values: {
									unionid: that.shopInfo.unionid,
									shop_code: that.shopInfo.shop_code,
									page: 1,
									is_home: 1
								}
							}
						},
						function (res) {
							if (res.code == 0) {
								that.collageList = res.data;
							} else {
								api.toast({
								    msg: res.message,
								    duration: 2000,
								    location: 'bottom'
								});
							}
						}
					);
				},

				// 获取热门商品
				getHotGoodsList: function () {
					var that = this;
					app.ajax(
						service['getGoodsList'],
						{
							data: {
								values: {
									unionid: that.shopInfo.unionid,
									shop_code: that.shopInfo.shop_code,
									is_home: 1
								}
							}
						},
						function (res) {
							if (res.code == 0) {
								that.hotGoodsList = res.data;
							} else {
								api.toast({
										msg: res.message,
										duration: 2000,
										location: 'bottom'
								});
							}
						}
					);
				},

				// 获取全部商品
				getAllGoodsList: function () {
					var that = this;
					app.ajax(
						service['getGoodsList'],
						{
							data: {
								values: {
									unionid: that.shopInfo.unionid,
									shop_code: that.shopInfo.shop_code,
									is_home: 2
								}
							}
						},
						function (res) {
							if (res.code == 0) {
								that.goodsList = res.data;
							} else {
								api.toast({
										msg: res.message,
										duration: 2000,
										location: 'bottom'
								});
							}
						}
					);
				},

				// 获取yzc商品
				getYzcGoodsList: function () {
					var that = this;
					app.ajax(
						service['getYzcShopGoodsList'],
						{
							data: {
								values: {
									unionid: that.shopInfo.unionid,
									shop_code: that.shopInfo.shop_code,
									page: 1,
									is_home: 1
								}
							}
						},
						function (res) {
							if (res.code == 0) {
								that.yzcGoods = res.data;
							} else {
								api.toast({
								    msg: res.message,
								    duration: 2000,
								    location: 'bottom'
								});
							}
						}
					);
				},

				// 获取所有分类
				getClassifyList: function () {
					var that = this;
					app.ajax(
						service['getclassifyList'],
						{
							data: {
								values: {
									unionid: that.shopInfo.unionid,
									shop_code: that.shopInfo.shop_code,
									is_home: 1,
									hot: 0
								}
							}
						},
						function (res) {
							if (res.code == 0) {
								that.classifyList = res.data;
							} else {
								api.toast({
										msg: res.message,
										duration: 2000,
										location: 'bottom'
								});
							}
						}
					);
				},

				// 检查是否已经收藏该店铺
				checkIsCollect: function () {
					var that = this;
					app.ajax(
						service['checkIsCollect'],
						{
							data: {
								values: {
									unionid: $api.getStorage("shop_unionid"),
									shop_id: that.shopInfo.id,
									type: 2,
									bs: this.bs
								}
							}
						},
						function (res) {
							if (res.code == 0) {
								if (res.data == 0) {
									that.isCollect = false;
								} else {
									that.isCollect = true;
									that.collectId = res.data;
								}
							} else {
								// api.toast({
								// 		msg: res.message,
								// 		duration: 2000,
								// 		location: 'bottom'
								// });
							}
						}
					);
				},

				// 显示分类列表
				showTypeList: function () {
					this.showType = !this.showType;
				},

				// 分类
				toClassify: function (id, name) {
					var that = this;
					var classfiyId = id;
					var name = name;
					app.openWin('buyClassfiy',{classfiyId:classfiyId, classfiyName: name, shopInfo: that.shopInfo},{reload: true});
				},

				// 查看更多
				toList: function (arg) {
					var that = this;
					if (arg == 'hot') {
						app.openWin('buyProductHot',{shopInfo: that.shopInfo, type: 'hot'},{reload: true});
					} else if (arg == 'all') {
						app.openWin('buyProductHot',{shopInfo: that.shopInfo, type: 'all'},{reload: true});
					} else if (arg == 'yzc') {
						app.openWin('buyProductHot',{shopInfo: that.shopInfo, type: 'yzc'},{reload: true});
					} else {
						app.openWin('buyGroup',{shopInfo: that.shopInfo},{reload: true}); // 拼团
					}
				},

				// 领取优惠券
				getDiscount: function (d_id, idx) {
					var that = this;
					var discount_id = d_id; // 优惠券列表
					if (this.bs == "") { //游客模式，没有缓存
							dialog.alert({
									title: '游客模式',
									msg: '体验更多功能，请先登录',
									buttons: ['取消', '确定']
							}, function(ret) {
									if (ret.buttonIndex == 2) { //确认
											api.closeToWin({
													name: 'WeChat_login'
											});
									}
							})
					}else{
						var isGet = $('.discountList').eq(idx).attr('data-get');
						if (isGet) {
							api.toast({
									msg: '您已领取该优惠券',
									duration: 2000,
									location: 'middle'
							});
							return;
						}
						app.ajax(
							service['getCouponDiscount'],
							{
								data: {
									values: {
										unionid: $api.getStorage("shop_unionid"), // 当前用户unionid
										coupon_discount_id: discount_id,
										shop_code: that.shopInfo.shop_code
									}
								}
							},
							function (res) {
								if (res.code == 0) {
									api.toast({
									    msg: '领取成功',
									    duration: 2000,
									    location: 'middle'
									});
									$('.discountList').eq(idx).css('background', 'gray');
									$('.discountList').eq(idx).attr('data-get', 'true');
								} else {
									api.toast({
											msg: res.message,
											duration: 2000,
											location: 'bottom'
									});
								}
							}
						);
					}

				},

				// 收藏\取消收藏当前店铺
				favorShop: function () {
					var that = this;
					var isCollect = that.isCollect;

					var url,arg;

					if (this.bs == "") { //游客模式，没有缓存
							dialog.alert({
									title: '游客模式',
									msg: '体验更多功能，请先登录',
									buttons: ['取消', '确定']
							}, function(ret) {
									if (ret.buttonIndex == 2) { //确认
											api.closeToWin({
													name: 'WeChat_login'
											});
									}
							})
					}else{
						if (isCollect) {
							// 取消收藏
							url = service['cancelCollectShop'];
							arg = {
								unionid: $api.getStorage("shop_unionid"),
								favorite_id: that.collectId
							}
						} else {
							// 添加收藏
							url = service['addToFavoriteShop'];
							arg = {
								unionid: $api.getStorage("shop_unionid"),
								type: 2,
								shop_id: that.shopInfo.id
							}
						}

						app.ajax(
							url,
							{
								data: {
									values: arg
								}
							},
							function (res) {
								if (res.code == 0) {
									if (!isCollect) {
										if (res.data) {
											that.collectId = res.data;
										}
									}
									that.isCollect = !that.isCollect;

								} else {
									api.toast({
											msg: res.message,
											duration: 2000,
											location: 'bottom'
									});
								}
							}
						);
					}

				},

				// 购物车
				toShoppingCart: function () {
					app.openWin('buyCart');
				},

				// 商品详情
				toGoodsDetail: function (goods_id, group, is_yzc) {
					var that = this;
					if (goods_id) {
						if (is_yzc) {
							app.openWin('buyGoodsYzc', {goodsId: goods_id});
						} else {
							if (group) {
								app.openWin('groupBuyDetail',{goodsId :goods_id});
							} else {
								if (is_yzc) {
									app.openWin('buyGoodsYzc', {goodsId: goods_id});
								} else {
									app.openWin('buyGoods', {goodsId: goods_id});
								}
							}
						}
					}
				},

				// 拼团详情
				toGroupDetail: function (id) {
					var that = this;
					app.openWin('groupBuyDetail',{goodsId :id});
				},

				// 联系卖家
				toChar: function () {
					var that = this;
					var obj = {
						openid: that.otherUserInfo.openid, //id
						nickname: that.shopInfo.shop_name, //昵称
						src: that.shopInfo.shop_logo, //头像
						new_nickname: that.otherUserInfo.new_nickname, //对象
					}
					if (this.bs == "") { //游客模式，没有缓存
							dialog.alert({
									title: '游客模式',
									msg: '体验更多功能，请先登录',
									buttons: ['取消', '确定']
							}, function(ret) {
									if (ret.buttonIndex == 2) { //确认
											api.closeToWin({
													name: 'WeChat_login'
											});
									}
							})
					}else{
						app.yz_chat(obj);
					}

				}
	  	},
			computed: {
				// 获取所在地省级地址
				locationChange: function () {
					return this.shopInfo.shop_location.split(' ')[1];
				}
			},
	  	mounted:function() {

	  	}
	};
	var even =function () {
		api.setStatusBarStyle({
        style: 'dark',
        color: '#asfsasfsa'
    })

		$('.buy-head-nav').on('click', function () {
			$('.nav-dio-top').toggle();
		});

		$('.nav-dio-top div').on('click', function () {
			var type = $(this).index();
			api.sendEvent({
			    name: 'shopIndexDoShomeThing',
			    extra: {
			        type: type,
			    }
			});

		});
	}
	var newVue = new Vue(vm);
	even();
});
}
